﻿using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace BehaviorsAndTriggers.Behaviors
{
    public class TextCleanBehavior : Behavior<Button>
    {

        /// <summary>
        /// the target is the textbox we want to clearn
        /// </summary>
        public TextBox Target
        {
            get { return (TextBox)GetValue(TargetProperty); }
            set { SetValue(TargetProperty, value); }
        }

        public static readonly DependencyProperty TargetProperty = DependencyProperty.Register(
            "Target",
            typeof(TextBox),
            typeof(TextCleanBehavior),
            new PropertyMetadata(null)
            );


        /// <summary>
        /// This method is called when the behavior is attached to an associated object.
        /// </summary>
        protected override void OnAttached()
        {
            AssociatedObject.Click += CleanText;
        }


        /// <summary>
        /// This method is called when the behavior is detached from its associated object.
        /// </summary>
        protected override void OnDetaching()
        {
            AssociatedObject.Click -= CleanText;
        }


        private void CleanText(object sender, RoutedEventArgs e)
        {
            if (Target != null)
            {
                Target.Text = string.Empty;
            }
            else
            {
                MessageBox.Show("Target is not set");
            }
        }
    }
}
